//
//  XKLCommentController.m
//  GuiZiShouDuanZi
//
//  Created by xmg on 16/5/8.
//  Copyright © 2016年 xiao_kl. All rights reserved.
//

#import "XKLCommentController.h"
#import "XKLEessenceItem.h"
#import <AFNetworking.h>
#import <MJRefresh.h>
#import <MJExtension.h>
#import "XKLTopCommetItem.h"
#import "XKLCommentCell.h"
#import "XKLessenceCell.h"

@interface XKLCommentController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *commentViewconstraint;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *commentView;

/**评论模型数组*/
@property (nonatomic,strong)NSMutableArray *commentItem;

/**热评*/
@property (nonatomic,strong)NSArray *hotCommentItem;
@end

@implementation XKLCommentController


static NSString *ID = @"commentCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self upTableView];
    [self upCommentView];
    [self upData];
    
}

-(void)upCommentView{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationCenter:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
}
-(void)notificationCenter:(NSNotification *)noti{
    
    //键盘显示/隐藏完毕的frame
    CGRect frame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    //获取变化时间
    CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //修改底部约束
    self.commentViewconstraint.constant = ScreenH - frame.origin.y;
    
    [UIView animateWithDuration:duration animations:^{
        [self.view layoutIfNeeded];
    }];
    
    
}

-(void)upTableView{
    //取消自动填充
    self.automaticallyAdjustsScrollViewInsets =NO;
    
    //填充
    self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    
    //设置tableView
    XKLessenceCell *cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([XKLessenceCell class]) owner:nil options:nil] lastObject];
    cell.item =self.item;
    self.tableView.tableHeaderView = cell;
    
    //自动计算cell的大小
    self.tableView.estimatedRowHeight = 50;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    //取消分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XKLCommentCell class]) bundle:nil] forCellReuseIdentifier:ID];
    self.navigationItem.title = @"评论";
    
    
    UIButton *but = [[UIButton alloc] init];
    [but setImage:[UIImage imageNamed:@"cellmorebtnnormal"] forState:UIControlStateNormal];
    [but setImage:[UIImage imageNamed:@"cellmorebtnclick"] forState:UIControlStateHighlighted];
    [but sizeToFit];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:but];
}

-(void)upData{
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    //设置根据下拉距离来设置透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
}

-(void)loadNewData{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"a"] = @"dataList";
    dic[@"c"] = @"comment";
    dic[@"data_id"] = @(self.item.ID);
    dic[@"hot"] = @1;
    [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.commentItem = [XKLTopCommetItem mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
        self.hotCommentItem = [XKLTopCommetItem mj_objectArrayWithKeyValuesArray:responseObject[@"hot"]];
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self.tableView.mj_header endRefreshing];
    }];
}

-(void)loadMoreData{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    if (self.hotCommentItem) {
        return 2;
    }else if (self.commentItem) {
        return 1;
    }else {
        return 0;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (self.hotCommentItem) {
        if (section == 0) {
            return self.hotCommentItem.count;
        }else{
            return self.commentItem.count;
        }
    }else if (self.commentItem){
        return self.commentItem.count;
        
    }
    return 0;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XKLCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (self.hotCommentItem) {
        if (indexPath.section == 0) {
            cell.item = self.hotCommentItem[indexPath.row];
        }else{
            cell.item = self.commentItem[indexPath.row];
        }
    }else if (self.commentItem){
        cell.item = self.commentItem[indexPath.row];
        
    }
    
    
    return cell;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    XKLLog(@"%ld",section);
    UILabel *label = [[UILabel alloc] init];
    label.text = @"热评";
    [label sizeToFit];
    return label;
}
//取消监听
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
